package airthmetic.exercise.map;

import java.util.HashMap;

/**
 * @Author douhy
 * @description map相关算法
 * @date 22/01/20
 */
public class MapDemo {

    // leetCode 1. 两数之和
    public static int[] twoSum(int[] nums, int target) {
        // 思路 1.嵌套循环
        //     2.结合map

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])){
                return new int[]{i, map.get(target - nums[i])};
            }else{
                map.put(nums[i],i);
            }
        }
        return null;
    }


    public static void main(String[] args) {
        int[] ints = {1, 2, 4, 6, 7, 9};
        for (int i : twoSum(ints, 3)) {
            System.out.println(i);
        }
    }
}
